$(document).ready(function()
{
    $("#pointsTable").tablesorter({
        sortList: [[0,0]],
        //sortForce: [[0,0]],
        headers: {
            1: {
                sorter: 'classimage'
            }
        },
        cssAsc: "headerUp ui-state-active",
        cssDesc: "headerDown ui-state-active",
        cssHeader: "headerUpDown",
        textExtraction: function(node) {
            // extract data from markup and return it
            if (node.childNodes[0].innerHTML)
                return node.childNodes[1].innerHTML;
            else
                return node.innerHTML;
        }
    });

    $("#raidTable").tablesorter({
        sortList: [[0,0]],
        //sortForce: [[0,0]],
        cssAsc: "headerUp ui-state-active",
        cssDesc: "headerDown ui-state-active",
        cssHeader: "headerUpDown",
        textExtraction: function(node) {
            // extract data from markup and return it
            if (node.childNodes[0].innerHTML)
                return node.childNodes[1].innerHTML;
            else
                return node.innerHTML;
        }
    });

    $("#pointsTable").bind(
        "sortStart",
        function()
        {
            var left = jQuery($(".filterDialog")).position().left;
            var top = jQuery($(".filterDialog")).position().top;
            $("#filterDialog").dialog('option', 'position', [left,top]);
            $(".playerdata").remove();
            $('#pointsTable').removeData('content_old');
            $("#filterDialog").dialog('option', 'position', [left,top]);
        });

    $("#pointsTable").bind(
        "sortEnd",
        function()
        {
            $(".headerUpDown > span").removeClass("ui-icon-triangle-2-n-s").addClass("ui-icon-triangle-2-n-s");
            $(".headerUp > span").addClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-2-n-s");
            $(".headerDown > span").addClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-2-n-s");
        });

    $("#raidTable").bind(
        "sortStart",
        function()
        {
            //var left = jQuery($(".filterDialog")).position().left;
            //var top = jQuery($(".filterDialog")).position().top;
            //$("#filterDialog").dialog('option', 'position', [left,top]);
            $(".raiddata").remove();
            $('#raidTable').removeData('content_old');
        //$("#filterDialog").dialog('option', 'position', [left,top]);
        });

    $("#raidTable").bind(
        "sortEnd",
        function()
        {
            $(".headerUpDown > span").removeClass("ui-icon-triangle-2-n-s").addClass("ui-icon-triangle-2-n-s");
            $(".headerUp > span").addClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-2-n-s");
            $(".headerDown > span").addClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-2-n-s");
        });

    var pointsTable_content_show;
    $("#pointsTable tr").click(
        function()
        {
            pointsTable_content_show = $(this).attr("id");
            filterDialogLeft = jQuery($(".filterDialog")).position().left;
            filterDialogTop = jQuery($(".filterDialog")).position().top;

            $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
            $("#filterDialog").dialog('close');
            if ($(".playerslide").size() > 0)
                $(".playerslide").slideUp("normal", pointsTablePartTwo)
            else
                pointsTablePartTwo();
        }
        );

    function pointsTablePartTwo()
    {
        $(".playerdata").remove();
        $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);

        if($('#pointsTable').data('content_old') == pointsTable_content_show)
        {
            $('#pointsTable').removeData('content_old');
        }
        else
        {
            $('#pointsTable').data('content_old', pointsTable_content_show);
            $('#pointsTable').data('content', $("."+pointsTable_content_show+"").html());
            $.ajax({
                method: "get",
                url: "<?php echo url_for('ajax/player') ?>",
                data: "p="+pointsTable_content_show.replace("player", "")+"&r=1",
                success: function(html)
                { //so, if data is retrieved, store it in html
                    //$("#"+pointsTable_content_show+"").hide(); //animation
                    $("#"+pointsTable_content_show+"").after(html); //show the html inside .content div
                    docReady('#tabsPointsAjax');
                    $(".playerdata").hide();
                    $(".playerslide").hide();
                    $(".playerdata").show();

                    //$("#"+pointsTable_content_show+"").fadeIn("slow", function() { $("#filterDialog").dialog('option', 'position', [left,top]); } ); //animation
                    $(".playerslide").slideDown("normal", function() {
                        $('#tabs3 > div').equalHeights();
                        $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
                    } );
                    $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
                }
            }); //close $.ajax(
        }
    }

    var raidTable_content_show;
    $("#raidTable tr").click(
        function()
        {
            raidTable_content_show = $(this).attr("id");
            //filterDialogLeft = jQuery($(".filterDialog")).position().left;
            //filterDialogTop = jQuery($(".filterDialog")).position().top;

            //$("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
            //$("#filterDialog").dialog('close');
            if ($(".raidslide").size() > 0)
                $(".raidslide").slideUp("normal", raidTablePartTwo)
            else
                raidTablePartTwo();
        }
        );

    function raidTablePartTwo()
    {
        $(".raiddata").remove();
        $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);

        if($('#raidTable').data('content_old') == raidTable_content_show)
        {
            $('#raidTable').removeData('content_old');
        }
        else
        {
            $('#raidTable').data('content_old', raidTable_content_show);
            $('#raidTable').data('content', $("."+raidTable_content_show+"").html());
            $.ajax({
                method: "get",
                url: "<?php echo url_for('ajax/raid') ?>",
                data: "p="+raidTable_content_show.replace("raid", "")+"&r=1",
                success: function(html)
                { //so, if data is retrieved, store it in html
                    //$("#"+raidTable_content_show+"").hide(); //animation
                    $("#"+raidTable_content_show+"").after(html); //show the html inside .content div
                    $(".raiddata").hide();
                    $(".raidslide").hide();
                    $(".raiddata").show();
                    docReady('#tabsRaidAjax');

                    //$("#"+raidTable_content_show+"").fadeIn("slow", function() { $("#filterDialog").dialog('option', 'position', [left,top]); } ); //animation
                    $(".raidslide").slideDown("normal", function() {
                        $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
                    } );
                    $("#filterDialog").dialog('option', 'position', [filterDialogLeft,filterDialogTop]);
                }
            }); //close $.ajax(
        }
    }

    //$(".headerUpDown > span").removeClass("ui-icon-triangle-2-n-s").addClass("ui-icon-triangle-2-n-s");
    //$(".headerUp > span").addClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-2-n-s");
    //$(".headerDown > span").addClass("ui-icon-triangle-1-s").removeClass("ui-icon-triangle-1-n").removeClass("ui-icon-triangle-2-n-s");

});
